package com.ies.dx.dex.file;

import com.ies.dx.util.Hex;
import com.ies.dx.util.Input;

public class header_item extends dex_item
{
    public final static byte[] DEX_FILE_MAGIC = {0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00};
    public final static int ENDIAN_CONSTANT = 0x12345678;
    public final static int REVERSE_ENDIAN_CONSTANT = 0x78563412;

    
    private byte[] magic = new byte[8];
    private byte[] checksum = new byte[4];
    private byte[] signature = new byte[20];
    private int file_size;
    private int header_size;
    private int endian_tag;
    private int link_size;
    private int link_off;
    private int map_off;
    private int string_ids_size;
    private int string_ids_off;
    private int type_ids_size; 
    private int type_ids_off;  
    private int proto_ids_size; 
    private int proto_ids_off;  
    private int field_ids_size; 
    private int field_ids_off; 
    private int method_ids_size; 
    private int method_ids_off; 
    private int class_defs_size; 
    private int class_defs_off;  
    private int data_size; 
    private int data_off;  
    
    public header_item(dex_file file)
    {
        super(file);
    }
    
    public int getStringIdsOffset()
    {
        return string_ids_off;
    }
    
    public int getStringIdsSize()
    {
        return string_ids_size;
    }
    
    public int getTypeIdsOffset()
    {
        return type_ids_off;
    }
    
    public int getTypeIdsSize()
    {
        return type_ids_size;
    }
    
    public int getProtoIdsSize()
    {
        return proto_ids_size;
    }
    
    public int getProtoIdsOff()
    {
        return proto_ids_off;
    }

    public int getFieldIdsSize()
    {
        return field_ids_size;
    }
    
    public int getFieldIdsOff()
    {
        return field_ids_off;
    }

    public int getMethodIdsSize()
    {
        return method_ids_size;
    }
    
    public int getMethodIdsOff()
    {
        return method_ids_off;
    }

    public int getClassDefsOff()
    {
        return class_defs_off;
    }
    
    public int getClassDefsSize()
    {
        return class_defs_size;
    }

    public int getDataOff()
    {
        return data_off;
    }
    
    public int getDataSize()
    {
        return data_size;
    }

    public void read(Input dis)
    {
        try
        {
            dis.read(magic);
            dis.read(checksum);
            dis.read(signature);
            file_size = dis.readInt();
            header_size = dis.readInt();
            endian_tag = dis.readInt();
            link_size = dis.readInt();
            link_off = dis.readInt();
            map_off = dis.readInt();
            string_ids_size = dis.readInt();
            string_ids_off = dis.readInt();
            type_ids_size = dis.readInt();
            type_ids_off = dis.readInt();
            proto_ids_size = dis.readInt();
            proto_ids_off = dis.readInt();
            field_ids_size = dis.readInt();
            field_ids_off = dis.readInt();
            method_ids_size = dis.readInt();
            method_ids_off = dis.readInt();
            class_defs_size = dis.readInt();
            class_defs_off = dis.readInt();
            data_size = dis.readInt();
            data_off = dis.readInt();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public void show()
    {
        System.out.println("------------------------------------------------------------------");
        System.out.print("[magic]             : " + Hex.dump(magic, 0, magic.length, 0, 128, 2));
        System.out.print("[checksum]          : " + Hex.dump(checksum, 0, checksum.length, 0, 128, 2));
        System.out.print("[signature]         : " + Hex.dump(signature, 0, signature.length, 0, 128, 2));
        System.out.println("[file_size]         : " + file_size + " (0x" + Hex.u4(file_size) + ")");
        System.out.println("[header_size]       : " + header_size + " (0x" + Hex.u4(header_size) + ")");      
        if (endian_tag == ENDIAN_CONSTANT)            
            System.out.println("[endian]            : ENDIAN_CONSTANT");
        else
            System.out.println("[endian]            : RERVERSE_ENDIAN_CONSTANT");
        System.out.println("[link_size]         : " + link_size + " (0x" + Hex.u4(link_size) + ")");
        System.out.println("[link_off]          : " + link_off + " (0x" + Hex.u4(link_off) + ")");
        System.out.println("[map_off]           : " + map_off + " (0x" + Hex.u4(map_off) + ")");
        System.out.println("[string_ids_size]   : " + string_ids_size + " (0x" + Hex.u4(string_ids_size) + ")");
        System.out.println("[string_ids_off]    : " + string_ids_off + " (0x" + Hex.u4(string_ids_off) + ")");
        System.out.println("[type_ids_size]     : " + type_ids_size + " (0x" + Hex.u4(type_ids_size) + ")");
        System.out.println("[type_ids_off]      : " + type_ids_off + " (0x" + Hex.u4(type_ids_off) + ")");
        System.out.println("[proto_ids_size]    : " + proto_ids_size + " (0x" + Hex.u4(proto_ids_size) + ")");
        System.out.println("[proto_ids_off]     : " + proto_ids_off + " (0x" + Hex.u4(proto_ids_off) + ")");
        System.out.println("[field_ids_size]    : " + field_ids_size + " (0x" + Hex.u4(field_ids_size) + ")");
        System.out.println("[field_ids_off]     : " + field_ids_off + " (0x" + Hex.u4(field_ids_off) + ")");
        System.out.println("[method_ids_size]   : " + method_ids_size + " (0x" + Hex.u4(method_ids_size) + ")");
        System.out.println("[method_ids_off]    : " + method_ids_off + " (0x" + Hex.u4(method_ids_off) + ")");
        System.out.println("[class_defs_size]   : " + class_defs_size + " (0x" + Hex.u4(class_defs_size) + ")");
        System.out.println("[class_defs_off]    : " + class_defs_off + " (0x" + Hex.u4(class_defs_off) + ")");
        System.out.println("[data_size]         : " + data_size + " (0x" + Hex.u4(data_size) + ")");
        System.out.println("[data_off]          : " + data_off + " (0x" + Hex.u4(data_off) + ")");
        System.out.println("------------------------------------------------------------------");
    }

    public String getData() 
    {
        return null;
    }

    @Override
    public void readData(Input dis)
    {
        // TODO Auto-generated method stub
        
    }
}
